Tuesday, November 23, 2004

Custom Collection Properties

We see many properties in .Net controls which have collection of items...
Example can be items collection for drop down list which contains selected
as boolean; Name as string and value as string...

If you need collection properties for your class or custom control...
This is what you do...
First make a class whose collection you require... Say for example you need collection of Names, and each name is a class having three other properties like FirstName, LastName and MiddleName... So you first will have to make your class "Name" and then the next class "NameCollection"...
The name collection will inherit from CollectionBase...
Then you override the required functions to achieve your functionality...

Below link will help you learn more...
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemcollectionscollectionbaseclasstopic.asp


With Best Regards,
Mitesh Mehta
Email : miteshvmehta@gmail.com
http://cc.1asphost.com/miteshvmehta/

Serialization Constructor

For your class to be serialized by Xml Serializer it is needed that it has a
default constructor... For your class, if you only have a constructor which
takes some fixed arguments and does not have a constructor which takes in no
arguments the serializer is not able to create an instance of your class to
serialize and so it fails with an error..

An unhandled exception of type 'System.InvalidOperationException' occurred
in system.xml.dll

Additional information: ApplicationName.ClassName cannot be serialized
because it does not have a default public constructor.

Similarly error might also occur in case you are using Activator class's
Create instance method for some type which does not have default public
constructor and you are not passing the constructor parameters...

So in short if it is not harming the base functionality it is always good to
have default constructor for your class...

With Best Regards,
Mitesh Mehta
Email : miteshvmehta@gmail.com
http://cc.1asphost.com/miteshvmehta/